<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wcs__postwms}">
      <wcs-form v-model="dataForm" :formList="formList" :table-column="tableColumn" :advanced="advanced"
        @check-change="checkChange" @changeAdvance="changeAdvance" @getDataList="getDataList" @getReset="getReset">
        <el-form-item>
          <el-button v-if="$hasPermission('wcs:post-wms:analyze')" type="primary" @click="postHandle('REPEAT')">{{
            '指令重发'
          }}</el-button>
          <el-button v-if="$hasPermission('wcs:post-wms:delete')" type="danger" @click="deleteHandle()">{{
              '指令删除'
            }}</el-button>
        </el-form-item>
        
      </wcs-form>
      <wcs-table ref="wcsTable" class="postWmsTable" v-model="dataForm" :page="page" :total="total" :limit="limit" :tableData="dataList"
        :table-column="column" :module-options="mixinViewModuleOptions" :dataListLoading="dataListLoading"
        :showSelection="true" @pageSizeChangeHandle="pageSizeChangeHandle"
        @pageCurrentChangeHandle="pageCurrentChangeHandle" @selectChangeHandle="selectChangeHandle" />
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import debounce from "lodash/debounce";
import { formList, tableColumn } from './config'
import wcsTable from '@/components/wcs-table'
import wcsForm from '@/components/wcs-form'
import { postHandle, deletePostWms } from '@/utils/api'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wcs/post-wms/page",
        getDataListIsPage: true,
      },
      id: '',
      dataForm: {
        contNo: '',
        curPos: '',
        noticeType: ''
      },
      formList,
      tableColumn,
      column: [],
      advanced: false
    };
  },
  components: {
    wcsTable,
    wcsForm
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
      this.id = ''; // 重置时清空选中ID
    },
    postHandle(operateType) {
      console.log(operateType + "，id为" + this.id)
      if (!this.id) {
        return this.$message({
          message: '请先选择任务！',
          type: 'warning',
          duration: 500
        })
      }
      this.$confirm(`是否确认对任务ID[${this.id}]进行[${operateType}]操作?`, this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(()=>{
            const params = {
              postId: this.id,
              operateType
            };
      postHandle(params).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$message({
          message: res.data,
          type: 'success',
          duration: 1000,
          onClose: () => {
                this.getDataList()
              }
        })
      })
      }).catch(()=>{})
    },

    deleteHandle() {
      console.log("删除，id为" + this.id)
      if (!this.id) {
        return this.$message({
          message: '请先选择要删除的任务！',
          type: 'warning',
          duration: 500
        })
      }

      this.$confirm(`是否确认删除任务ID[${this.id}]?`, this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        // 构造参数，后端需要 Long[] ids 数组
        const ids = [this.id];

        deletePostWms(ids).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.getDataList(); // 刷新列表
              this.id = ''; // 清空选中ID
            }
          })
        }).catch(error => {
          console.error('删除失败:', error);
          this.$message.error('删除失败');
        });
      }).catch(() => {
        // 用户取消删除
      });
    },
     // 单选
     selectChangeHandle(selection) {
       if (selection.length > 1) {
         const lastSelected = selection[selection.length - 1];
         this.$refs.wcsTable.$refs.Table.clearSelection();
         this.$refs.wcsTable.$refs.Table.toggleRowSelection(lastSelected, true);
         this.id = lastSelected.postId;
       } else if (selection.length === 1) {
         this.id = selection[0].postId;
       } else {
         this.id = '';
       }

       console.log('当前选中ID:', this.id);
    }
  },
 
};
</script>
<style lang="scss" scoped>
/* 隐藏表头的全选框 */
::v-deep .postWmsTable thead .el-table-column--selection .cell {
  display: none;
}
</style>


